package com.tianzeculture.stagebao.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.network.util.ToastUtil;

/**
 * Created by Administrator on 2018/5/24 0024.
 */

public class ChoiceJobDialog {


    ImageView ivArtistsCheck, ivMerchantsCheck, ivChoice;
    private Dialog dialog;
    private OnSelectClickListener onSelectClickListener;

    public ChoiceJobDialog(Context context) {
        init(context);
    }

    private void init(final Context context) {
        dialog = new Dialog(context, R.style.DialogPushUpInAnimStyle);
        dialog.setContentView(R.layout.dialog_choice_job);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);

        Window window = dialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        window.setAttributes(layoutParams);

        FrameLayout layoutArtists = (FrameLayout) window.findViewById(R.id.layoutArtists);
        layoutArtists.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setChoiceState(1);
            }
        });
        FrameLayout layoutMerchants = (FrameLayout) window.findViewById(R.id.layoutMerchants);
        layoutMerchants.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setChoiceState(2);
            }
        });
        ivArtistsCheck = (ImageView) window.findViewById(R.id.ivArtistsCheck);
        ivMerchantsCheck = (ImageView) window.findViewById(R.id.ivMerchantsCheck);
        ivChoice = (ImageView) window.findViewById(R.id.ivChoice);
        ivChoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSelectClickListener != null) {
                    if (state == 1 || state == 2)
                        onSelectClickListener.onOk(state);
                    else
                        ToastUtil.showShort(context, "请选择身份完成注册");
                }
                dismiss();
            }
        });

    }


    int state = 0;//0未选择 1艺人 2商家

    /**
     * 设置选择状态
     *
     * @param state 0未选择 1艺人 2商家
     */
    public void setChoiceState(int state) {
        this.state = state;
        boolean canClick;
        if (state == 1) {
            ivArtistsCheck.setVisibility(View.VISIBLE);
            ivMerchantsCheck.setVisibility(View.INVISIBLE);
            canClick = true;
        } else if (state == 2) {
            ivArtistsCheck.setVisibility(View.INVISIBLE);
            ivMerchantsCheck.setVisibility(View.VISIBLE);
            canClick = true;
        } else {
            ivArtistsCheck.setVisibility(View.INVISIBLE);
            ivMerchantsCheck.setVisibility(View.INVISIBLE);
            canClick = false;
        }
        choiceButtonState(canClick);
    }

    /**
     * 确认选择按钮状态
     *
     * @param canClick true可以点击 false 不可点击
     */
    private void choiceButtonState(boolean canClick) {
        if (canClick)
            ivChoice.setEnabled(true);
        else
            ivChoice.setEnabled(false);
    }


    /**
     *
     */
    public void show() {
        dialog.show();
    }

    /**
     *
     */
    public void dismiss() {
        dialog.dismiss();
        if (onSelectClickListener != null) {
            onSelectClickListener.onCancel();
        }
    }

    public OnSelectClickListener getOnSelectClickListener() {
        return onSelectClickListener;
    }

    public void setOnSelectClickListener(OnSelectClickListener onSelectClickListener) {
        this.onSelectClickListener = onSelectClickListener;
    }


    public interface OnSelectClickListener {
        void onOk(int state);

        void onCancel();
    }
}
